<?php

//  Wtg
//  Copyright (C) 2006 Niels Egberts <niels.egberts@gmail.com>
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//  02111-1307, USA.

if ( !defined('IS_GAME') ) {
	die ("Hacking attempt");
}

function register_form () {
	if (isset($_SESSION['username'])) 
	{
		$output .= "You are already logged in so there is no point in registrating again.";
		return $output;
	}

	if ( isset($_POST['filled_register_form']) )
	{
		$username = $_POST['username'];
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		$email = $_POST['email'];

		$valid = 1; // As long as the input is valid, it stays 1
		
		// Check If something is not filled in
		if ( ($valid == 1) AND ( ($username == "") OR ($password1 == "") OR ($password2 == "") OR ($email == "") ) ) 
		{
			$output .= "<p class=\"error\">Something is not filled in, please go back and correct the problems</p>";
			$valid = 0; 
		}
		if ( ($valid == 1) AND ( ( $password1 != $password2 ) ) )
		{
			$output .= "<p class=\"error\">The 2 passwords arn't equal to eachother</p>";
			$valid = 0;
		}
		if ( ($valid == 1) AND (strlen($username) > 32 ) )
		{
			$output .= "<p class=\"error\">Your username is too long, please keep it below 32 characters</p>";
			$valid = 0;
		}
		if ( ($valid == 1) AND (strlen($password1) > 32 ) )
		{
			$output .= "<p class=\"error\">Your password is too long, please keep it below 32 characters</p>";
			$valid = 0;
		}
		if ( ($valid == 1) AND (strlen($email) > 32 ) )
		{
			$output .= "<p class=\"error\">Your email adress is too long, please keep it below 64 characters</p>";
			$valid = 0;
		}

		// Check if the username is already occupied
		if ($valid == 1) {
			$query = "SELECT id FROM users WHERE username='$username'";
			$result = mysql_query($query);
			$num = mysql_numrows($result);
			if ($num > 0) 
			{ 
				$output .= "<p class=\"error\">That username is already taken, please press back and try again.</p>"; 
				$valid=0; 
			}
		}


		// Check if the email adress is not already used
		if ($valid == 1) {
			$query = "SELECT id FROM users WHERE email='$email'";
			$result = mysql_query($query);
			$num = mysql_numrows($result);
			if ($num > 0) 
			{ 
				$output .= "<p class=\"error\">The given email adress is already registrated.</p>"; 
				$valid=0; 
			}
		}
	
		if ($valid == 1) 
		{
			// De gebruiker invoeren in de tabel registrated
			$query = "INSERT INTO users VALUES ('', '$username', '$password1', '$email', 'normal')";
			mysql_query($query);
	
			$output .= "<p>Congratulations, you are registrated!</p>";
		}


	} else {
		$output .= "<form action=\"".$_SERVER['REQUEST_URI']."\" method=\"post\">";
		$output .= <<<EOF
		<p>Username: <input type="text" name="username" /></p>
		<p>Password: <input type="password" name="password1" /></p>
		<p>Repeat password: <input type="password" name="password2" /></p>
		<p>E-mail adress: <input type="text" name="email" /></p>
		<p><input type="hidden" name="filled_register_form" /></p>
		<p><input type="submit" value="Submit" /></p>
		</form>
EOF;
		}
	return $output;
}
	
?>
